package com.bootdo.wx.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.service.GoodsGoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.GoodsTeamDO;
import com.bootdo.wx.service.GoodsTeamService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 秒杀商品列表
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-10-11 10:27:37
 */
 
@Controller
@RequestMapping("/wx/goodsTeam")
public class GoodsTeamController {
	@Autowired
	private GoodsTeamService goodsTeamService;
	@Autowired
	private GoodsGoodsService goodsGoodsService;
	
	@GetMapping()
	@RequiresPermissions("wx:goodsTeam:goodsTeam")
	String GoodsTeam(){
	    return "wx/goodsTeam/goodsTeam";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:goodsTeam:goodsTeam")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<Map<String,Object>> goodsTeamList = goodsTeamService.getTeamList(query);
		int total = goodsTeamService.getTeamListCount(query);
		PageUtils pageUtils = new PageUtils(goodsTeamList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:goodsTeam:add")
	String add(){
	    return "wx/goodsTeam/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:goodsTeam:edit")
	String edit(@PathVariable("id") String id,Model model){
		GoodsTeamDO goodsTeam = goodsTeamService.get(id);
		Map<String,Object> map = new HashMap<String,Object>();
		List<Map<String,Object>> list = goodsGoodsService.queryAllForIdAndName(map);
		String goodsId = goodsTeam.getGoodsId();
		map.put("goodsId", goodsId);
		Map<String,Object> oldMap = goodsGoodsService.queryAllForIdAndName(map).get(0);
		model.addAttribute("goodsList", list);
		model.addAttribute("goodsTeam", goodsTeam);
		model.addAttribute("oldMap", oldMap);
	    return "wx/goodsTeam/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:goodsTeam:add")
	public R save( GoodsTeamDO goodsTeam){
		Date date = new Date();
		goodsTeam.setId(UuidUtil.get32UUID());
		goodsTeam.setDelFlag(0);
		goodsTeam.setCreateAt(date);
		goodsTeam.setNumSale(0);
		if(goodsTeamService.save(goodsTeam)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:goodsTeam:edit")
	public R update( GoodsTeamDO goodsTeam){
		goodsTeamService.update(goodsTeam);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:goodsTeam:remove")
	public R remove( String id){
		if(goodsTeamService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:goodsTeam:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
		goodsTeamService.batchRemove(ids);
		return R.ok();
	}
	
}
